//
// Created by denglibin on 19-10-22.
//
/**
 *进程组id
 **/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/wait.h>

 static int main_(){

    pid_t  pid = fork();
    if(pid > 0){ // 父
        printf("parent, getpgrp() %d\n", getpgrp()); // 进程组id
        printf("parent, getpgid() %d\n", getpgid(getpid()));// 进程组id
        printf("parent session id %d\n", getsid(0)); // 会话id
        sleep(1);
        setpgid(pid, pid); // 设置子进程的组id(把进程加入到一个进程组)
        wait(NULL);
    } else{
        printf("child, getpgrp() %d\n", getpgrp());
        printf("child, getpgid() %d\n", getpgid(getpid()));
        sleep(2);
        printf("after set child getpgrp() %d\n", getpgrp());

        printf("child session id %d\n", getsid(0)); // 会话id

        setsid();// 子进程非组长进程，故其成为新回话首进程，且成为组长进程 调用setid()将创建一个会话期和新的进程组，调用进程为该会话期和进程组组长，并且该进程没有控制终端
        printf("child pid %d\n", getpid());
        printf("child group id %d\n", getpgrp());
        printf("child session id %d\n", getsid(0));
    }
    return 0;
}

